package com.anbinh.doctor.fragment.clinic;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.anbinh.doctor.DetailtActivity;
import com.anbinh.doctor.R;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.ProfileResponse.Data.Profile;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;
import com.zcw.togglebutton.ToggleButton;
import com.zcw.togglebutton.ToggleButton.OnToggleChanged;

public class FragmentAddDoctor extends SherlockFragment {
	private static FragmentAddDoctor fragment = null;
	private static boolean mIsUpdate;
	private View mView;
	private View mLoadding;
	private UserLogin userLogin;
	private TextView lblGender;
	private TextView lblNgaysinh;
	private EditText edPhone;

	private String mBrithday = "";
	private int mGender = 1;

	private EditText edEmail;
	private Profile profile;
	private ToggleButton gender;
	private EditText edName;
	private int doctor_id;
	private String mEmail;
	private View lineEmail;

	public static FragmentAddDoctor newInstance(boolean isUpdate) {
		if (fragment == null) {
			fragment = new FragmentAddDoctor();
		}
		mIsUpdate = isUpdate;
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		pustData(mIsUpdate);
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,
			com.actionbarsherlock.view.MenuInflater inflater) {
		menu.add(0,1000, Menu.NONE, "Hoành thành").setIcon(
				R.drawable.ic_done);

		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public void onResume() {
		super.onResume();
		if (mIsUpdate) {
			((DetailtActivity) getActivity()).setActionBarTitle(getResources()
					.getString(R.string.cap_thong_tin_bac_si));
		} else {
			((DetailtActivity) getActivity()).setActionBarTitle(getResources()
					.getString(R.string.them_bac_si));
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_add_doctor, null);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Utils.setOnTouchHideKeyboard(getActivity(), mView);
		gender = (ToggleButton) mView.findViewById(R.id.gengerToggle);
		mLoadding = (View) mView.findViewById(R.id.loadding);

		lblNgaysinh = (TextView) mView.findViewById(R.id.lblNgaysinh);
		lblNgaysinh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DialogFragment newFragment = new DatePickerFragment(lblNgaysinh);
				newFragment.show(getFragmentManager(), "datePicker");
			}
		});

		gender.setOnToggleChanged(new OnToggleChanged() {

			@Override
			public void onToggle(boolean on) {
				if (on) {
					lblGender.setText("Nam");
					mGender = 1;
				} else {
					mGender = 0;
					lblGender.setText("Nữ");
				}
				// updateInfo();
			}
		});

		edName = (EditText) mView.findViewById(R.id.lblName);
		edPhone = (EditText) mView.findViewById(R.id.edPhone);
		edEmail = (EditText) mView.findViewById(R.id.edEmail);

		lineEmail = (View) mView.findViewById(R.id.lineEmail);
		if (!mIsUpdate) {
			lineEmail.setVisibility(View.GONE);
		} else {
			lineEmail.setVisibility(View.VISIBLE);
		}
		lblGender = (TextView) mView.findViewById(R.id.lblGender);

		userLogin = Utils.getAccountModel(getActivity(), Utils.KEY_USER);
		new CountDownTimer(500, 500) {

			@Override
			public void onTick(long millisUntilFinished) {

			}

			@Override
			public void onFinish() {
				Utils.hideKeyboard(getActivity());
			}
		}.start();

	}

	public static class DatePickerFragment extends DialogFragment implements
			DatePickerDialog.OnDateSetListener {
		TextView textView;

		public DatePickerFragment(TextView textView) {
			this.textView = textView;
		}

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			// Use the current date as the default date in the picker
			final Calendar c = Calendar.getInstance();
			int year = c.get(Calendar.YEAR);
			int month = c.get(Calendar.MONTH);
			int day = c.get(Calendar.DAY_OF_MONTH);

			// Create a new instance of DatePickerDialog and return it
			return new DatePickerDialog(getActivity(), this, year, month, day);
		}

		public void onDateSet(DatePicker view, int year, int month, int day) {
			month++;
			textView.setText(year + "-" + month + "-" + day);
		}
	}

	public void pustData(boolean isUpdate) {
		mLoadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);
		String mFull_name = edName.getText().toString().trim();
		String mPhone = edPhone.getText().toString().trim();
		mBrithday = lblNgaysinh.getText().toString().trim();
		if (!isUpdate) {
			doctor_id = -1;
			mEmail = "";
		} else {
			doctor_id = FragmentDoctorList.newInstance().getCurrentDoctors()
					.getId();
			mEmail = edEmail.getText().toString().trim();
		}

		ServerController.getInstance(getActivity()).requestPushDoctor(
				userLogin.getData().getToken(), mFull_name, mPhone, mGender,
				mBrithday, doctor_id, mEmail, new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						mLoadding.setVisibility(View.INVISIBLE);

						if (getActivity() == null)
							return;
						if (data.getResponsecode() > 0) {
							Intent intent = new Intent();
							intent.putExtra(Utils.KEY, Utils.ADD_DOCTOR);
							getActivity().setResult(Integer.MAX_VALUE, intent);
							getActivity().finish();
						}

					}

					@Override
					public void onError(int errorCode, String message) {
						mLoadding.setVisibility(View.INVISIBLE);
					}

				});
	}
}
